package com.maoye.ossp.common.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hotent.base.db.api.IdGenerator;
import com.maoye.ossp.sys.log.aspect.LoginStatus;
import com.maoye.ossp.sys.log.entity.SysLogLogin;
import com.maoye.ossp.sys.log.service.ISysLogLoginService;
import com.maoye.ossp.sys.log.service.impl.SysLogLoginServiceImpl;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.auth.core.SecurityHelper;
import com.maoye.sc.auth.core.authenticator.AccountSubject;
import com.maoye.sc.auth.core.authenticator.DefaultAccountSubject;
import com.maoye.sc.auth.core.authenticator.exception.AuthenticationException;
import com.maoye.sc.auth.local.token.AccountToken;

/**
 * 用户鉴权Contorller
 * 
 * @author zhuangwl
 * 
 */
@Controller
public class SimpleAuthenticatorController {

	AccountSubject accountSubject;

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	IdGenerator idGenerator;
	@Resource
	ISysLogLoginService IsysLogLoginService;
	
	@RequestMapping(value = "/welcome")
	public String welcome() {
		return "/test";
	}

	/**
	 * 管理登录
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (user.getId() != null) {
			return "redirect:/index";
		}
		return "/login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(String username, String password,
			HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		AccountToken at = new AccountToken();
		at.setAccount(username);
		at.setPassword(password);
		try {
			accountSubject = new DefaultAccountSubject();

			accountSubject.login(at);
			//登陆日志记录
			try {
				SysLogLogin log = new SysLogLogin();
				log.setId(idGenerator.getUId());
				log.setName(UserUtils.getCurrentUserId().toString());
				log.setOperateTime(new Date());
				log.setStatus(LoginStatus.online_);
				log.setLoginIp(request.getRemoteAddr());
				IsysLogLoginService.recordLogLogin(log);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			modelAndView.setViewName("redirect:/index");
		} catch (AuthenticationException e) {
			logger.error(e.getMessage());
			modelAndView.addObject("msg", "账号或密码错误");
			modelAndView.setViewName("/login");
		}
		return modelAndView;
	}

	/**
	 * 退出
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		AccountSubject accountSubject = (AccountSubject) SecurityHelper
				.getAccountSubject();
		//退出日志记录
		try {
			SysLogLogin log = new SysLogLogin();
			if(UserUtils.getCurrentUserId()==null){
				log.setName("");
			}
			else{
				log.setName(UserUtils.getCurrentUserId().toString());
			}
			log.setId(idGenerator.getUId());
			log.setOperateTime(new Date());
			log.setStatus(LoginStatus.offline_);
			log.setLoginIp(request.getRemoteAddr());
			IsysLogLoginService.recordLogLogin(log);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		if (accountSubject != null) {
			accountSubject.logout();	
		}
		return "/login";
	}

}
